package cn.cxj.system.controller;

import cn.cxj.basic.util.AjaxResult;
import cn.cxj.basic.util.LogAnnotation;
import cn.cxj.basic.util.PageList;
import cn.cxj.system.domain.DictionaryDetail;
import cn.cxj.system.domain.DictionaryType;
import cn.cxj.system.query.DictionaryDetailQuery;
import cn.cxj.system.service.IDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典明细接口
 * @author 陈鑫杰
 * @date 2022年2月12日09:52:39
 */
@RestController
@RequestMapping("/dictionaryDetail")
@Api(tags = "数据字典明细接口",description = "数据字典明细接口详细描述")
public class DictionaryDetailController {

    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    /**
     * 新增或者修改 POST
     * @param dictionaryDetail
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "数据字典明细添加或修改",notes = "如果有id就是修改否则就是添加")
    @LogAnnotation(operateType = "数据字典新增或者修改方法")
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if (dictionaryDetail.getId() == null) {
                dictionaryDetailService.add(dictionaryDetail);
            } else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 基于id删除 DELETE
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @LogAnnotation(operateType = "根据id删除")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 基于id查询 GET
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @LogAnnotation(operateType = "基于id查询")
    public DictionaryDetail findById(@PathVariable("id") Long id){
        return dictionaryDetailService.queryById(id);
    }

    /**
     * 查询全部 GET
     * @return
     */
    @GetMapping("/list")
    @LogAnnotation(operateType = "查询全部")
    public List<DictionaryDetail> findAll(){
        return dictionaryDetailService.queryAll();
    }


    /**
     * 分页和高级查询 POST
     * @param query
     * @return
     */
    @PostMapping("/page")
    @LogAnnotation(operateType = "分页和高级查询")
    public PageList<DictionaryDetail> pageQuery(@RequestBody DictionaryDetailQuery query){
        return dictionaryDetailService.queryPage(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @LogAnnotation(operateType = "批量删除")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            dictionaryDetailService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 基于typeId查询
     * @param dictionaryType
     * @return
     */
    @PostMapping("/queryByTypeId")
    @LogAnnotation(operateType = "数据字典基于类型id查询")
    public List<DictionaryDetail> queryByTypeId(@RequestBody DictionaryType dictionaryType){
        return dictionaryDetailService.queryByTypeId(dictionaryType.getId());
    }
}
